//
//  MoonHTTPRequest.h
//  DirectSeller
//
//  Created by 王 文彬 on 13-9-22.
//  Copyright (c) 2013年 Chongqing Fenrir Technology Co., Ltd. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MBProgressHUD.h"

typedef void (^SuccessBlock)(id result);
typedef void (^FailureBlock)(NSError* error);

typedef NS_ENUM(NSInteger, MoonHTTPBodyFormat) {
    MoonHTTPBodyFormatNormal = 0,
    MoonHTTPBodyFormatMultiPart
};

typedef NS_ENUM(NSInteger, MoonHTTPProcessorMode) {
    MoonHTTPProcessorModePlain
};

@protocol MoonHTTPRequestDelegate;

@interface MoonHTTPRequest : NSObject <MBProgressHUDDelegate>
{
//    NSURL*                             _URL;
    NSString*                          _URLString;
    MoonHTTPBodyFormat                 _HTTPBodyFormat;
    NSMutableDictionary*               _HTTPParameters;
    
    NSString*                          _clientCertificate;
    NSString*                          _clientCertificatePassword;
    
    __weak id<MoonHTTPRequestDelegate> _delegate;
    
    SEL                                _successSelector;
    SEL                                _failureSelector;
    
    SuccessBlock                       _successBlock;
    FailureBlock                       _failureBlock;
    
    NSString*                          _errorMessage;
    
    MBProgressHUD*                     _progressor;
    BOOL                               _progressOn;
    NSString*                          _progressTitle;
    NSString*                          _progressDetailText;
    MoonHTTPProcessorMode              _progressMode;
}


- (id)initWithURLString:(NSString *)URLString;
//- (id)initWithURL:(NSURL *)URL;

@property (nonatomic, strong) NSString* URLString;
@property (nonatomic, assign) MoonHTTPBodyFormat HTTPBodyFormat;
@property (nonatomic, weak)   id<MoonHTTPRequestDelegate> delegate;

@property (nonatomic, strong) NSString* clientCertificate;
@property (nonatomic, strong) NSString* clientCertificatePassword;

@property (nonatomic, assign) SEL successSelector;
@property (nonatomic, assign) SEL failureSelector;

@property (nonatomic, strong, readonly) NSString* errorMessage;

@property (nonatomic, strong, readonly) NSMutableDictionary* parameters;

- (void)setParameter:(NSObject *)parameter forName:(NSString *)parameterName;
- (void)addParameters:(NSDictionary *)parameters;

- (void)onSuccess:(SuccessBlock)success onFailure:(FailureBlock)error;
- (void)onSuccess:(SuccessBlock)success;
- (void)onFailure:(FailureBlock)failure;

- (void)asynchronous;


@property (nonatomic, assign) BOOL progressOn;
@property (nonatomic, strong) NSString* progressTitle;
@property (nonatomic, strong) NSString* progressDetailText;

@end


@protocol MoonHTTPRequestDelegate <NSObject>

@optional
- (void)moonHTTPRequest:(MoonHTTPRequest *)request didResponse:(id)result;
- (void)moonHTTPRequest:(MoonHTTPRequest *)request onError:(NSError *)error;

@end